﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="AddSchedulerTaskCmdlet.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace CloudBlitz.Powershell.Cmdlets
{
    using System;
    using System.Management.Automation;

    using HpcSchedulerManagement.DataContracts;

    [Cmdlet(VerbsCommon.Add, "SchedulerTask")]
    public class AddSchedulerTaskCmdlet : HpcSchedulerCmdletBase
    {
        [Parameter(Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public int JobId { get; set; }

        [Parameter(Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public SchedulerTask Task { get; set; }

        protected override void ProcessRecord()
        {
            try
            {
                base.ProcessRecord();
                this.AddSchedulerTaskProcess();
            }
            catch (Exception ex)
            {
                this.ThrowTerminatingError(this.HandleException(ex).Result);
            }
        }

        private void AddSchedulerTaskProcess()
        {
            var taskId = this.CreateClient().CreateTask(this.Credentials.ClusterName, this.JobId, this.Task).Result;

            this.WriteObject(taskId);
        }
    }
}